﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using PPAP.BO;

namespace PPAP.Web.Part
{
    public partial class Report : PageBase
    {
        PPARBO bo = new PPARBO();
        T_FileBO manager = new T_FileBO();
        SamplecardBO sbo = new SamplecardBO();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.QueryString["RID"] != null)
                {
                    DataSet ds = bo.GetReportByPPARid(Request.QueryString["RID"].ToString());
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        DataRow dr = ds.Tables[0].Rows[0];
                        lblPNO.Text = dr["bdntpartno"].ToString();
                        lblPName.Text = dr["bdntpartname"].ToString();
                        lblRevisionNo.Text = dr["bdntrevisionno"].ToString();
                        lblQLevel.Text = dr["qlevel"].ToString();
                        lblColor.Text = dr["color"].ToString();
                        //lblAddress.Text = dr["address"].ToString();
                        lblPlantName.Text = dr["plantname"].ToString();
                        lblTelephone.Text = dr["mobilephone"].ToString();
                        lblSupplierNo.Text = dr["suppliercode"].ToString();
                        lblSupplierName.Text = dr["suppliername"].ToString();
                        //lblStatus.Text = dr["bdntrevisionstatus"].ToString();
                        txtDecision.InnerHtml += dr["decisiontext"].ToString();
                        // txtExplanation.Text += dr["explanations"].ToString();
                        lblDate.Text = dr["endtime"].ToString() == "" ? "" : DateTime.Parse(dr["endtime"].ToString()).ToString("yyyy-MM-dd");
                        lblPerson.Text = dr["firstname"].ToString() + " " + dr["familyname"].ToString();
                        lblEmail.Text = dr["quanlityengineer"].ToString();
                        //lblCode.Text = dr["sort"].ToString();
                        lblDepartment.Text = dr["department"].ToString();
                        lblTestReport.Text = dr["samplingreportno"].ToString();
                        lblPPARNO.Text = dr["pparno"].ToString();
                        // txtExplanation.Text = dr["explanations"].ToString() == "" ? "Explanation:" + dr["remarks"].ToString() : "Explanation:" + dr["explanations"].ToString();
                        lblQM.Text = dr["quanlitymanager"].ToString();
                        lblConper.Text = dr["fname"].ToString() == "" ? "" : dr["fname"].ToString() + " " + dr["faname"].ToString();
                        //lblConperEm.Text = dr["cemail"].ToString() == "" ? "" : dr["cemail"].ToString();
                        //lblQVP.Text = dr["quanlityvp"].ToString();
                        imgvp.ImageUrl = "~/Part/image/" + dr["quanlityvp"].ToString() + ".png";
                        DataSet dsfile = manager.GetFileList(dr["DEVIATIONSHEET"].ToString());
                        DataSet ds2 = sbo.GetList(" s.pparid=" + Request.QueryString["rid"].ToString() + "  and s.status<>9 ");
                        int height = 80;
                        if (dr["explanations"].ToString().Trim() == "")  //如果数据库中未保存，则自动生成
                        {
                            foreach (DataRow drr in ds2.Tables[0].Rows)
                            {
                                if (drr["RESULT"].ToString() == "Y" || drr["RESULT"].ToString() == "R")
                                {
                                    height += 44;
                                    txtExplanation.InnerHtml += drr["testitemname"].ToString() + "<br>" + "Reason for deviation is " + drr["reason"].ToString() + "<br><br>";
                                }
                            }
                            //height = height > 42 ? height - 23 : height;
                        }
                        else
                        {
                            foreach (DataRow drr in ds2.Tables[0].Rows)
                            {
                                if (drr["RESULT"].ToString() == "Y" || drr["RESULT"].ToString() == "R")
                                {
                                    height += 44;
                                   // txtExplanation.Text += drr["testitemname"].ToString() + "\n" + "Reason for deviation is " + drr["reason"].ToString() + "\n\n";
                                }
                            }
                            txtExplanation.InnerHtml = dr["explanations"].ToString().Replace("\r\n", "<br>").Replace("\n", "<br>"); ;
                        }
                        //txtExplanation.Height = height < 80 ? 80 : height; ;
                        height = 20;
                        if (dr["DECISIONTEXT"].ToString().Trim() == "")//如果数据库中未保存，则自动生成
                        {
                            if (dr["elevel"].ToString() == "E")
                            {
                                txtDecision.InnerHtml = "No series delivery release can be issued.";
                            }
                            else
                            {

                                if (dr["finalresult"].ToString() == "G")
                                {
                                    txtDecision.InnerHtml = "The series delivery release has been issued";
                                }
                                if (dr["finalresult"].ToString() == "Y")
                                {
                                    txtDecision.InnerHtml = "Only a conditional series delivery release has been issued";
                                }
                                if (dr["finalresult"].ToString() == "R")
                                {
                                    txtDecision.InnerHtml = "The series delivery release has not been issued";
                                }
                                height += 23;
                                if (dr["partrelatedresult"].ToString() == "G")
                                {
                                    txtDecision.InnerHtml += "<br>" + "Product:deviations from our specifications have not been detected ";
                                }
                                if (dr["partrelatedresult"].ToString() == "Y")
                                {
                                    txtDecision.InnerHtml += "<br>" + "Product:deviations from our specifications have been detected ";
                                }
                                if (dr["partrelatedresult"].ToString() == "R")
                                {
                                    txtDecision.InnerHtml += "<br>" + "Product:customer relevant deviations from our specifications have been detected";
                                }
                                height += 23;

                                if (dr["processrelatedresult"].ToString() == "G")
                                {
                                    txtDecision.InnerHtml += "<Br>" + "Process:approval has been given";
                                }
                                if (dr["processrelatedresult"].ToString() == "Y")
                                {
                                    txtDecision.InnerHtml += "<br>" + "Process:deviations have been detected ";
                                }
                                if (dr["processrelatedresult"].ToString() == "R")
                                {
                                    txtDecision.InnerHtml += "<br>" + "Process:severe deviations have been detected";
                                }
                            }
                            if (dsfile.Tables[0].Rows.Count > 0 && dr["resubmissiondate"].ToString() != "" && DateTime.Parse(dr["resubmissiondate"].ToString()) > DateTime.Parse("1900-02-01"))
                            {
                                height += 23;
                                txtDecision.InnerHtml += "<br>We expect an optimized product and/or process as well as a new PPA-Report latest till the re-submission date " + DateTime.Parse(dr["resubmissiondate"].ToString()).ToString("yyyy-MM-dd");
                            }
                            // height = height > 42 ? height - 23 : height;
                        }
                        else
                        {
                            if (dr["elevel"].ToString() == "E")
                            {
                                //txtDecision.Text = "No series delivery release can be issued.";
                            }
                            else
                            {

                               
                                height += 44;
                               
                                height += 44;

                               
                            }
                            if (dsfile.Tables[0].Rows.Count > 0 && dr["resubmissiondate"].ToString() != "" && DateTime.Parse(dr["resubmissiondate"].ToString()) > DateTime.Parse("1900-02-01"))
                            {
                                height += 44;
                                //txtDecision.Text += "\nWe expect an optimized product and/or process as well as a new PPA-Report latest till the re-submission date " + DateTime.Parse(dr["resubmissiondate"].ToString()).ToString("yyyy-MM-dd");
                            }
                            txtDecision.InnerHtml = dr["DECISIONTEXT"].ToString().Replace("\r\n", "<br>").Replace("\n", "<br>"); ;
                        }
                        //txtDecision.Height = height < 80 ? 80 : height;
                    }
                }
            }
        }
    }
}